package com.yanjiang;

import java.sql.*;

public class BaseDao1 {
    static Connection connection =null;
    static ResultSet resultSet = null;
    static PreparedStatement preparedStatement=null;
    static Statement statement = null;
    //创建新连接
    public static  Connection getConnection(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://127.0.0.1:3306/kgcnews";
            String username="root";
            String password="123456";
            connection=DriverManager.getConnection(url,username,password);
        }catch (Exception e){
            e.printStackTrace();
        }
        return connection;
    }

    //查
    public static ResultSet selectObj(String sql,Object[] object){
        try{
            connection = BaseDao.getConnection();
            preparedStatement=connection.prepareStatement(sql);
            if(object!=null){
                for (int i=0;i<object.length;i++){
                    preparedStatement.setObject(i+1,object[i]);
                }
                preparedStatement.executeQuery();
            }
            preparedStatement.executeQuery();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            Connection connection =null;
            ResultSet resultSet = null;
            PreparedStatement preparedStatement=null;
            Statement statement = null;
            BaseDao.closeConnect(connection, preparedStatement, statement, resultSet);
        }
        return resultSet;

    }





    //关闭连接
    public static void  CloseConnection(Connection connection,ResultSet resultSet,PreparedStatement preparedStatement,Statement statement){
        try{
            if (resultSet!=null){
                resultSet.close();
            }
            if (preparedStatement!=null){
                preparedStatement.close();
            }
            if (statement!=null){
                statement.close();
            }
            if (connection!=null){
                connection.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
